package com.ld.common.util.notice.service;

import java.util.List;

import com.ld.admin.model.system.SysUserRolesModel;
import com.ld.admin.model.usercenter.SysUserNoticeMediaModel;
import com.ld.shieldsb.common.core.collections.ListUtils;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.DaoFactory;
import com.ld.shieldsb.common.web.StaticParams;
import com.ld.shieldsb.dao.TransactionImplBaseDao;
import com.ld.shieldsb.dao.model.QueryModel;

import lombok.extern.slf4j.Slf4j;

/**
 * 发送通知服务项目实现
 * 
 * @ClassName ProjectNoticeSendService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年1月30日 下午1:38:32
 *
 */
@Slf4j
public class ProjectNoticeSendService extends NoticeSendService {
    public String defaultDataSource = "db0."; // 默认值
    private TransactionImplBaseDao BaseDAO;

    public ProjectNoticeSendService() {
        init();
    }

    protected void init() {
    }

    // 发给用户
    @Override
    public Result sendToUser(String[] noticeType, String title, String content, String userId) {
        return send2User(noticeType, title, content, userId);
    }

    /**
     * 查询用户通知方式并发送通知
     * 
     * @Title send2User
     * @author 吕凯
     * @date 2019年4月8日 上午10:14:21
     * @param noticeTypes
     *            通知类型
     * @param title
     * @param content
     * @param userId
     *            void
     */
    private Result send2User(String[] noticeTypes, String title, String content, String userId) {
        Result result = new Result();
        if (noticeTypes != null) {
            QueryModel queryNoticeModel = new QueryModel();
            queryNoticeModel.addEq(StaticParams.DB_FILED_STATE, StaticParams.STATE_NORMAL);
            queryNoticeModel.addInSet("mediaType", noticeTypes);
            queryNoticeModel.addEq("userId", userId);
            List<SysUserNoticeMediaModel> noticModels = getBaseDAO().getList(SysUserNoticeMediaModel.class, queryNoticeModel);
            if (ListUtils.isNotEmpty(noticModels)) {
                for (SysUserNoticeMediaModel userNoticeMedia : noticModels) {
                    String sendTo = userNoticeMedia.getSendTo();
                    result = sendNotice(title, content, userNoticeMedia.getMediaType(), sendTo);
                }
            }
        }
        return result;
    }

    // 发给群组（角色）
    @Override
    public Result sendToGroup(String[] noticeTypes, String title, String content, String groupId) {
        Result result = new Result();
        // 1、查询组内用户，
        QueryModel queryUsersModel = new QueryModel();
//        queryUsersModel.addEq(StaticParams.DB_FILED_STATE, StaticParams.STATE_NORMAL);
        queryUsersModel.addEq("roleId", groupId);
        List<SysUserRolesModel> roleUsers = getBaseDAO().getList(SysUserRolesModel.class, queryUsersModel);
        // 2、查询用户通知方式，
        // 3、发送通知
        if (ListUtils.isNotEmpty(roleUsers)) {
            for (SysUserRolesModel userRoles : roleUsers) {
                result = send2User(noticeTypes, title, content, userRoles.getUserId() + "");
            }
        }
        return result;
    }

    public TransactionImplBaseDao getBaseDAO() {
        if (BaseDAO == null) {
            String dataSourceValue = defaultDataSource; // 默认值
            log.debug("dataSource==" + dataSourceValue + " " + this.getClass());
            BaseDAO = (TransactionImplBaseDao) DaoFactory.getDao(dataSourceValue);
        }
        return BaseDAO;
    }

}
